package com.example.javabasic.thread.classone.demo3;

/**
 * 中断线程
 */
public class ThreadClose {

    public static void main(String[] args) {
        test1();
    }


    /**
     * 关闭线程1
     */
    public static void test1() {
        ThreadTestClose close = new ThreadTestClose();
        close.start();
        try {
            //五秒钟后关闭线程
            Thread.sleep(5000);
            close.shutDown();
        } catch (InterruptedException e) {
            System.out.println("异常1");
        }
    }

    public static void test2() {

    }

    public static void test3() {

    }

    public static void test4() {

    }

    /**
     * 线程内部类
     */
    static class ThreadTestClose extends Thread{
        private boolean status = true;
        @Override
        public void run() {
            System.out.println("线程开始运行");
            while(status){
                try {
                    Thread.sleep(1000);
                    System.out.println("线程执行一次");
                } catch (InterruptedException e) {
                    System.out.println("线程中断异常");
                    break;
                }
            }

        }

        public void shutDown(){
            System.out.println("调用关闭线程");
//            this.interrupt();
            this.status = false;
        }
    }
}
